package com.dinggou.shopjb.utils;

import android.content.Context;
import android.text.TextUtils;

import com.dinggou.shopjb.bean.UserInfoBean;

import java.util.Random;

public class DataUtils {
    public static final String AGREE_STATE = "agree_state";//隐私协议同意状态
    public static final String PUSH_FIRST_STATE = "push_first_state";//推广状态
    public static final String CLIENT_ID = "client_id";//渠道id
    public static final String CLIENT_NAME = "client_name";//渠道name
    public static final String USER_TOKEN = "user_token";//token
    public static final String UUID = "uuid";//uuid通用唯一标识
    public static final String SIGN_KEY = "sign_key";//渠道密钥
    public static final String OAID = "oaid";//oaid匿名设备标识
    public static final String USER_LOGIN = "userLogin";//是否登录
    public static final String USER_ID = "user_id";//用户id
    public static final String NICK_NAME = "nickname";//用户名
    public static final String PAY_PASS = "pay_pass";//资金密码
    public static final String MOBILE = "mobile";//手机号
    public static final String REG = "reg";//注册
    public static final String SP_ACCESS_AGREEMENT = "sp_access_agreement";//是否同意隐私政策
    public static final String USER_INFO = "user_info";//用户信息
    public static final String SP_HAS_MESSAGE = "sp_has_message";//是否有消息
    public static final String DEFAULT_MODE = "default_mode";//默认的展示模式，true默认，false简洁
    public static final String SP_PRO_CODE_POSITION = "sp_pro_code";
    public static final String SP_FOLLOW_ORDER_EXPLAIN = "sp_follow_order_explain";//跟单提示
    public static final String SP_GUIDE_BUY = "guide_buy";//新手单下单后推荐充值弹窗
    public static final String SP_FIRST_INTO = "sp_first_into";//首次进入应用弹窗
    public static final String SP_FIRST_TAKE_MONEY = "sp_first_take_money";//首次提现弹窗
    public static final String SP_AGREE_PERMISSION = "sp_agree_permission";//同意权限

    public static void setAgreePermission(Context context, boolean flag){
        SharedPreferencesUtil.getInstance(context).putBoolean(SP_AGREE_PERMISSION, flag);
    }

    public static boolean getAgreePermisssion(Context context){
        return SharedPreferencesUtil.getInstance(context).getBoolean(SP_AGREE_PERMISSION);
    }



    public static void setFirstTakeMoney(Context context, boolean flag){
        SharedPreferencesUtil.getInstance(context).putBoolean(SP_FIRST_TAKE_MONEY, flag);
    }

    public static boolean getFirstTakeMoney(Context context){
        return SharedPreferencesUtil.getInstance(context).getBoolean(SP_FIRST_TAKE_MONEY);
    }

    public static void setFirstInto(Context context, boolean flag){
        SharedPreferencesUtil.getInstance(context).putBoolean(SP_FIRST_INTO, flag);
    }

    public static boolean getFirstInto(Context context){
        return SharedPreferencesUtil.getInstance(context).getBoolean(SP_FIRST_INTO);
    }

    public static void setGuideBuy(Context context, boolean flag) {
        SharedPreferencesUtil.getInstance(context).putBoolean(SP_GUIDE_BUY, flag);
    }

    public static boolean getGuideBuy(Context context) {
        return SharedPreferencesUtil.getInstance(context).getBoolean(SP_GUIDE_BUY);
    }


    public static void setUserLevel(Context context, int arg) {
        SharedPreferencesUtil.getInstance(context).putInt("lv", arg);
    }

    public static int getUserLevel(Context context) {
        return SharedPreferencesUtil.getInstance(context).getInt("lv");
    }

    public static String getAvailableBalance(Context context) {
        String balance = SharedPreferencesUtil.getInstance(context).getString("available_balance");
        return "".equals(balance) ? "0" : balance;
    }

    public static void setAvailableBalance(Context context, String available_balance) {
        SharedPreferencesUtil.getInstance(context).putString("available_balance", available_balance);
    }

    public static void setIntSPByTag(Context context, String tag, int arg) {
        SharedPreferencesUtil.getInstance(context).putInt(tag, arg);
    }


    public static int getIntSPByTag(Context context, String tag) {
        return SharedPreferencesUtil.getInstance(context).getInt(tag);
    }

    /**
     * 设置布尔类型的值
     *
     * @param context
     * @param tag
     * @param arg
     */
    public static void setBooleanSPByTag(Context context, String tag, boolean arg) {
        SharedPreferencesUtil.getInstance(context).putBoolean(tag, arg);
    }

    /**
     * 获取布尔类型的值
     *
     * @param context
     * @param tag
     * @return
     */
    public static boolean getBooleanSPByTag(Context context, String tag) {
        return SharedPreferencesUtil.getInstance(context).getBoolean(tag);
    }

    public static void setAgreeState(Context context, boolean flag) {
        SharedPreferencesUtil.getInstance(context).putBoolean(AGREE_STATE, flag);
    }

    public static boolean getAgreeState(Context context) {
        return SharedPreferencesUtil.getInstance(context).getBoolean(AGREE_STATE);
    }

    public static void setPushFirstState(Context context, boolean flag) {
        SharedPreferencesUtil.getInstance(context).putBoolean(PUSH_FIRST_STATE, flag);
    }

    public static boolean getPushFirstState(Context context) {
        return SharedPreferencesUtil.getInstance(context).getBoolean(PUSH_FIRST_STATE);
    }

    public static void setClientId(Context context, String arg) {
        SharedPreferencesUtil.getInstance(context).putString(CLIENT_ID, arg);
    }

    public static String getClientId(Context context) {
        return SharedPreferencesUtil.getInstance(context).getString(CLIENT_ID);
    }
    public static void setClientName(Context context, String arg) {
        SharedPreferencesUtil.getInstance(context).putString(CLIENT_NAME, arg);
    }

    public static String getClientName(Context context) {
        return SharedPreferencesUtil.getInstance(context).getString(CLIENT_NAME);
    }

    public static void setToken(Context context, String arg) {
        SharedPreferencesUtil.getInstance(context).putString(USER_TOKEN, arg);
    }

    public static String getToken(Context context) {
        return SharedPreferencesUtil.getInstance(context).getString(USER_TOKEN);
    }

    public static void setUserId(Context context, String arg) {
        SharedPreferencesUtil.getInstance(context).putString(USER_ID, arg);
    }

    public static String getUserId(Context context) {
        String uid = SharedPreferencesUtil.getInstance(context).getString(USER_ID);
        return uid.isEmpty() ? "0" : uid;
    }

    public static void setUUID(Context context, String arg) {
        SharedPreferencesUtil.getInstance(context).putString(UUID, arg);
    }

    public static String getUUID(Context context) {
        return SharedPreferencesUtil.getInstance(context).getString(UUID);
    }

    public static void setOAID(Context context, String arg) {
        SharedPreferencesUtil.getInstance(context).putString(OAID, arg);
    }

    public static String getOAID(Context context) {
        return SharedPreferencesUtil.getInstance(context).getString(OAID);
    }

    public static void setSignKey(Context context, String arg) {
        SharedPreferencesUtil.getInstance(context).putString(SIGN_KEY, arg);
    }

    public static String getSignKey(Context context) {
        return SharedPreferencesUtil.getInstance(context).getString(SIGN_KEY);
    }

    public static void setLogin(Context context, boolean flag) {
        SharedPreferencesUtil.getInstance(context).putBoolean(USER_LOGIN, flag);
    }

    public static boolean isLogin(Context context) {
        return SharedPreferencesUtil.getInstance(context).getBoolean(USER_LOGIN);
    }

    public static void setNickName(Context context, String arg) {
        SharedPreferencesUtil.getInstance(context).putString(NICK_NAME, arg);
    }

    public static String getNickName(Context context) {
        return SharedPreferencesUtil.getInstance(context).getString(NICK_NAME);
    }

    public static void setIsSetPayPass(Context context, boolean arg) {
        SharedPreferencesUtil.getInstance(context).putBoolean(PAY_PASS, arg);
    }

    public static boolean getIsSetPayPass(Context context) {
        return SharedPreferencesUtil.getInstance(context).getBoolean(PAY_PASS);
    }

    public static void setMobile(Context context, String arg) {
        SharedPreferencesUtil.getInstance(context).putString(MOBILE, arg);
    }

    public static String getMobile(Context context) {
        return SharedPreferencesUtil.getInstance(context).getString(MOBILE);
    }

    public static void setRegister(Context context, boolean flag) {
        SharedPreferencesUtil.getInstance(context).putBoolean(REG, flag);
    }

    public static boolean isRegister(Context context) {
        return SharedPreferencesUtil.getInstance(context).getBoolean(REG);
    }

    /**
     * 设置用户信息
     *
     * @param context
     * @param userInfo
     */
    public static void setUserInfo(Context context, UserInfoBean userInfo) {
        setMobile(context, userInfo.mobile);
        setNickName(context, userInfo.nickname);
        setIsSetPayPass(context, userInfo.isSetFundPwd);
        SharedPreferencesUtil.getInstance(context).putObject(USER_INFO, userInfo);
    }

    public static UserInfoBean getUserInfo(Context context) {
        return SharedPreferencesUtil.getInstance(context).getObject(USER_INFO, UserInfoBean.class);
    }

    /**
     * 清除登录信息
     *
     * @param context
     */
    public static void clearUserInfo(Context context) {
        setLogin(context, false);
        setToken(context, "");
        setUserId(context, "0");
        setMobile(context, "");
        setNickName(context, "");
        setIsSetPayPass(context, false);
        SharedPreferencesUtil.getInstance(context).putObject(USER_INFO, null);
    }

    /**
     * 登录成功保存登录信息
     *
     * @param context
     * @param userInfoBean
     */
    public static void LoginSucess(Context context, UserInfoBean userInfoBean) {
        DataUtils.setRegister(context, true);
        DataUtils.setLogin(context, true);
        DataUtils.setUserId(context, userInfoBean.user_id);
        DataUtils.setToken(context, userInfoBean.token);
    }

    /**
     * 获取设备Id
     *
     * @param context
     * @return
     */
    public static String getDeviceId(Context context) {
        String uuid = getUUID(context);
        if (!TextUtils.isEmpty(uuid)) {
            return uuid;
        }
        String imei = Utils.getIMEI(context);
        if (TextUtils.isEmpty(imei) || imei.contains("0000")) {
            uuid = System.currentTimeMillis() + "-" + new Random().nextInt(99999) + 10000 + "-" + new Random().nextInt(99999) + 10000;
        }
        return uuid;
    }

}
